package com.jifeng.sterilizer.utils;

import cn.hutool.core.util.ReUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jifeng.sterilizer.entity.VcodeEntity;
import com.jifeng.sterilizer.service.VcodeService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtils {

    /**
     * 判断是否是身份证格式
     * @param idcard 身份证
     * @return boolean
     */
    public static boolean isIdCard(String idcard) {
        return ReUtil.isMatch("[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}$|^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X)", idcard);
    }


    /**
     * 判断是否是最长4位汉字的名字
     * @param name 名字
     * @return boolean
     */
    public static boolean isFourName(String name) {
        return ReUtil.isMatch("[\u4E00-\u9FA5]{1,4}", name);
    }

    /**
     * 判断是否是6位数字的密码
     * @param password 密码
     * @return boolean
     */
    public static boolean isPassword(String password) {
        return ReUtil.isMatch("[0-9]{6}", password);
    }

    /**
     * 判断是否是手机号码
     * @param account 手机号
     * @return boolean
     */
    public static boolean isPhone(String account) {
        return ReUtil.isMatch("1[0-9]{10}", account);
    }
    // 判断是否是全数字
    public static boolean isAllNumer(String str) {
        //Pattern pattern = Pattern.compile("^-?[0-9]+"); //这个也行
        Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");//这个也行
        Matcher isNum = pattern.matcher(str);
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    // 判断是否是10位数并且以规定的数字开头
    public static boolean isNumber(String str, int type) {
        try {
            Pattern pattern = Pattern.compile("^-?\\d+(\\.\\d+)?$");//这个也行
            Matcher isNum = pattern.matcher(str);
            if (!isNum.matches() || str.length() != 10) {
                return false;
            }
            if (type == 0) {
                return str.startsWith("0");
            } else if (type == 1) {
                return str.startsWith("1");
            } else if ((type == 2)) {
                return str.startsWith("2");
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean isCode(String account, String code, VcodeService vcodeService) {
        try {
            List<VcodeEntity> vcodeEntityList = vcodeService.list(new QueryWrapper<VcodeEntity>().eq("account", account).eq("enable", 1).orderByDesc("id"));
            String vcode2 = "";
            if (vcodeEntityList.size() > 0) {
                VcodeEntity vcodeEntity = vcodeEntityList.get(0);
                vcode2 = vcodeEntity.getCode();
                if (!code.equals(vcode2)) {
                    return false;
                }
                return true;
            } else {
                return false;
            }
        } catch (Exception e) {
            return false;
        }
    }
}
